home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / rayshade / libshade / surfdef.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  3KB  |  130 lines

  1. /*
  2.  * surfdef.c
  3.  *
  4.  * Copyright (C) 1989, 1991, Craig E. Kolb
  5.  * All rights reserved.
  6.  *
  7.  * This software may be freely copied, modified, and redistributed
  8.  * provided that this copyright notice is preserved on all copies.
  9.  *
  10.  * You may not distribute this software, in whole or in part, as part of
  11.  * any commercial product without the express consent of the authors.
  12.  *
  13.  * There is no warranty or other guarantee of fitness of this software
  14.  * for any purpose.  It is provided solely "as is".
  15.  *
  16.  * $Id: surfdef.c,v 4.0 91/07/17 14:47:53 kolb Exp Locker: kolb $
  17.  *
  18.  * $Log:    surfdef.c,v $
  19.  * Revision 4.0  91/07/17  14:47:53  kolb
  20.  * Initial version.
  21.  * 
  22.  */
  23. #include "rayshade.h"
  24. #include "libsurf/surface.h"
  25.  
  26. static Surface *Surfaces;    /* Named surfaces */
  27.  
  28. Surface DefaultSurface = {
  29.         "DeFault",        /* name */
  30.         {0.1, 0.1, 0.1},    /* ambient */
  31.         {0.6, 0.6, 0.6},    /* diffuse */
  32.         {0.5, 0.5, 0.5},    /* specular */
  33.         {0.0, 0.0, 0.0},    /* Diffuse transmission 'curve' */
  34.         {1.0, 1.0, 1.0},    /* Specular transmission 'curve' */
  35.         12.,            /* reflected Phong coef */
  36.         12.,            /* transmitted Phong coef */
  37.         1.,            /* spec. transmitted attenuation */
  38.         DEFAULT_INDEX,        /* index of refr */
  39.         0.,            /* reflectivity */
  40.         0.,            /* transparency */
  41.         0.,            /* translucency */
  42.         FALSE,            /* noshadow */
  43.         NULL,            /* next */
  44. };
  45.  
  46. Surface *SurfaceGetNamed(), *SurfaceFetchNamed();
  47.  
  48. /*
  49.  * Add surf to the list of defined surfaces.
  50.  */
  51. void
  52. SurfaceAddToDefined(surf)
  53. Surface *surf;
  54. {
  55.     /*
  56.      * Make sure index of refraction isn't bogus.
  57.      */
  58.     if (surf->transp > EPSILON && surf->index <= 0.)
  59.         RLerror(RL_PANIC,
  60.             "Index of refraction must be positive.\n");
  61.  
  62.     if (surf->name == (char *)NULL || *surf->name == (char)NULL)
  63.         RLerror(RL_PANIC, "Surface with NULL name defined.\n");
  64.  
  65.     if (SurfaceFetchNamed(surf->name) != (Surface *)NULL)
  66.         RLerror(RL_WARN,
  67.             "Redefinition of \"%s\" surface.", surf->name);
  68.  
  69.     surf->next = Surfaces;
  70.     Surfaces = surf;
  71. }
  72.  
  73. /*
  74.  * Search for surface with given name.  If not found, complain and exit.
  75.  */
  76. Surface *
  77. SurfaceGetNamed(name)
  78. char *name;
  79. {
  80.     Surface *stmp;
  81.  
  82.     stmp = SurfaceFetchNamed(name);
  83.     if (stmp == (Surface *)NULL)
  84.         RLerror(RL_PANIC, "Undefined surface \"%s\".", name);
  85.  
  86.     return stmp;
  87. }
  88.  
  89. /*
  90.  * Return pointer to surface with given name, NULL if no such surface.
  91.  */
  92. Surface *
  93. SurfaceFetchNamed(name)
  94. char *name;
  95. {
  96.     Surface *stmp;
  97.  
  98.     for (stmp = Surfaces; stmp ; stmp = stmp->next)
  99.         if(strcmp(name, stmp->name) == 0)
  100.             return stmp;
  101.     /*
  102.      * No surface named "name".
  103.      */
  104.     return (Surface *)NULL;
  105. }
  106.  
  107. /*
  108.  * Traverse the given hitlist to find the "bottom-most" surface.
  109.  * If no surface is found, use the default.
  110.  */
  111. Surface *
  112. GetShadingSurf(hitlist)
  113. HitList *hitlist;
  114. {
  115.     int i;
  116.  
  117.     /*
  118.      * -1 here because the World always has a NULL surface
  119.      * (DefaultSurf is used instead)
  120.      */
  121.     for (i = 0; i < hitlist->nodes -1; i++) {
  122.         if (hitlist->data[i].obj->surf)
  123.             return hitlist->data[i].obj->surf;
  124.     }
  125.     /*
  126.      * No suface found -- use the default.
  127.      */
  128.     return &DefaultSurface;
  129. }
  130.